home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / plugins / events.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  131 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from gi.repository import GObject
  5. from quodlibet import util
  6. from quodlibet.plugins import PluginHandler
  7. from quodlibet.util.songwrapper import SongWrapper, ListWrapper
  8. from quodlibet.util.songwrapper import check_wrapper_changed
  9. from quodlibet.util import connect_obj
  10.  
  11. class EventPlugin(object):
  12.     '''Plugins that run in the background and receive events.
  13.  
  14.     Event plugins, unlike other plugins, are instantiated on startup and
  15.     the same instance is used even when the plugin is enabled or disabled.
  16.  
  17.     Callables:
  18.         obj.plugin_on_song_started(song)
  19.         obj.plugin_on_song_ended(song, stopped)
  20.         obj.plugin_on_added([song1, song2, ...])
  21.         obj.plugin_on_changed([song1, song2, ...])
  22.         obj.plugin_on_removed([song1, song2, ...])
  23.         obj.plugin_on_paused()
  24.         obj.plugin_on_unpaused()
  25.         obj.plugin_on_seek(song, msec)
  26.         obj.plugin_on_error(song, error)
  27.     '''
  28.     PLUGIN_INSTANCE = True
  29.     
  30.     def enabled(self):
  31.         '''Called when the plugin is enabled.'''
  32.         pass
  33.  
  34.     
  35.     def disabled(self):
  36.         '''Called when the plugin is disabled.'''
  37.         pass
  38.  
  39.  
  40.  
  41. def list_signal_names(type_):
  42.     '''List of supported signal names for a GType, instance or class'''
  43.     type_ = getattr(type_, '__gtype__', type_)
  44.     names = []
  45.     if not type_.is_instantiatable() and not type_.is_interface():
  46.         return names
  47.     None.extend(GObject.signal_list_names(type_))
  48.     if type_.parent:
  49.         names.extend(list_signal_names(type_.parent))
  50.     for iface in type_.interfaces:
  51.         names.extend(list_signal_names(iface))
  52.     
  53.     return names
  54.  
  55.  
  56. def _map_signals(obj, prefix = 'plugin_on_', blacklist = None):
  57.     sigs = list_signal_names(obj)
  58.     if blacklist is None:
  59.         blacklist = []
  60.     sigs = [ s for s in sigs if s not in blacklist ]
  61.     sigs = [ (s, prefix + s.replace('-', '_')) for s in sigs ]
  62.     return sigs
  63.  
  64.  
  65. class EventPluginHandler(PluginHandler):
  66.     
  67.     def __init__(self, librarian = None, player = None):
  68.         if librarian:
  69.             sigs = _map_signals(librarian, blacklist = ('notify',))
  70.             for event, handle in sigs:
  71.                 
  72.                 def handler(librarian, *args):
  73.                     self._EventPluginHandler__invoke(librarian, args[-1], *args[:-1])
  74.  
  75.                 librarian.connect(event, handler, event)
  76.             
  77.         if librarian and player:
  78.             sigs = _map_signals(player, blacklist = ('notify',))
  79.             for event, handle in sigs:
  80.                 
  81.                 def cb_handler(librarian, *args):
  82.                     self._EventPluginHandler__invoke(librarian, args[-1], *args[:-1])
  83.  
  84.                 connect_obj(player, event, cb_handler, librarian, event)
  85.             
  86.         self._EventPluginHandler__plugins = { }
  87.  
  88.     
  89.     def __invoke(self, librarian, event, *args):
  90.         args = list(args)
  91.         if args and args[0]:
  92.             if isinstance(args[0], dict):
  93.                 args[0] = SongWrapper(args[0])
  94.             elif isinstance(args[0], (set, list)):
  95.                 args[0] = ListWrapper(args[0])
  96.             
  97.         for plugin in self._EventPluginHandler__plugins.itervalues():
  98.             method_name = 'plugin_on_' + event.replace('-', '_')
  99.             handler = getattr(plugin, method_name, None)
  100.             if handler is not None:
  101.                 
  102.                 try:
  103.                     handler(*args)
  104.                 except Exception:
  105.                     util.print_exc()
  106.                 
  107.  
  108.         if event not in ('removed', 'changed') and args:
  109.             app = app
  110.             import quodlibet
  111.             songs = args[0]
  112.             if not isinstance(songs, (set, list)):
  113.                 songs = [
  114.                     songs]
  115.             songs = filter(None, songs)
  116.             check_wrapper_changed(librarian, app.window, songs)
  117.  
  118.     
  119.     def plugin_handle(self, plugin):
  120.         return issubclass(plugin.cls, EventPlugin)
  121.  
  122.     
  123.     def plugin_enable(self, plugin):
  124.         self._EventPluginHandler__plugins[plugin.cls] = plugin.get_instance()
  125.  
  126.     
  127.     def plugin_disable(self, plugin):
  128.         self._EventPluginHandler__plugins.pop(plugin.cls)
  129.  
  130.  
  131.